package com.hudongwx.yuan_sdjj.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.app.FragmentTabHost;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hudongwx.yuan_sdjj.R;

import org.xutils.view.annotation.ContentView;

/**
 * Created by hudongwx on 16-10-27.
 */
public class InputView extends RelativeLayout {
    private EditText editText;
    private ImageView imageView,ivIcon ;
    private String myhint,mytext;
    private int myColor,mySize,myIcon;
    public InputView(Context context) {
        super(context);
        initView(context);
    }

    public InputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        //设置自定义属性后，取得相应的值
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.my_input_view);
        myhint =array.getString(R.styleable.my_input_view_myhint);
        mytext =array.getString(R.styleable.my_input_view_mytext);
        myColor =array.getColor(R.styleable.my_input_view_mycolor, Color.BLACK);
        mySize =array.getDimensionPixelSize(R.styleable.my_input_view_mysize,10);
        myIcon=array.getResourceId(R.styleable.my_input_view_myicon,0);
        //设置给自身的控件
        ivIcon.setImageResource(myIcon);
        editText.setTextColor(myColor);
        editText.setTextSize(mySize);
        editText.setHint(myhint);
        //        //随机回收
        array.recycle();
    }
    //初始化控件
    public void initView (Context context){
        View view = LayoutInflater.from(context).inflate(R.layout.input_view, this, true);
         editText= (EditText) view.findViewById(R.id.input_view_et);
        imageView = (ImageView) view.findViewById(R.id.input_view_iv);
        ivIcon = (ImageView) view.findViewById(R.id.input_view_iv2);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(TextUtils.isEmpty(s)){
                    imageView.setVisibility(INVISIBLE);
                }else {
                    imageView.setVisibility(VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editText.setText("");
            }
        });

    }
    public ImageView getIvIcon() {
        return ivIcon;
    }

    public void setIvIcon(ImageView ivIcon) {
        this.ivIcon = ivIcon;
    }

    public String getMyhint() {
        return myhint;
    }

    public void setMyhint(String myhint) {
        this.myhint = myhint;
    }

    public String getMytext() {
        return mytext;
    }

    public void setMytext(String mytext) {
        this.mytext = mytext;
    }

    public int getMyColor() {
        return myColor;
    }

    public void setMyColor(int myColor) {
        this.myColor = myColor;
    }

    public int getMySize() {
        return mySize;
    }

    public void setMySize(int mySize) {
        this.mySize = mySize;
    }

    public int getMyIcon() {
        return myIcon;
    }

    public void setMyIcon(int myIcon) {
        this.myIcon = myIcon;
    }
    public void setInputType(int inputType){
        editText.setInputType(inputType);
    }
    public int getInputType(){
        return editText.getInputType();
    }

}
